import json
import pandas as pd
import requests as rq
import copy

def main():
    LL = []
    df = pd.read_excel('communities.xlsx', sheet_name='Sheet1', encoding='utf-8')
    print(len(df), df.size, list(df.columns))

    with open('wuhan_gaode.json','r') as fr:
        all = json.loads(fr.read())

    dL = []
    ct = 0
    for district in all['districts'][0]['districts']:
        dName = district['name']
        for c in district['districts']:
            ct += 1
    print('cttt',ct)
            # cName = c['name']
            # center = c['center'].split(',')
            # dL.append([dName, cName.replace('街街','街').replace('百步亭花园','百步亭社区'), float(center[0]), float(center[1])])

    out = {'communities':{}}
    ct2 = 0
    for row in range(0, len(df)):
        print(str(df.iloc[row, 0]), str(df.iloc[row, 1]))
        if str(df.iloc[row, 0]) != 'nan':
            thisDistrict = copy.deepcopy(df.iloc[row, 0])
            out['communities'][thisDistrict] = {}
            out['communities'][thisDistrict]['tel'] = []
        else:
            try:
                a = int(str(df.iloc[row, 1]))
                out['communities'][thisDistrict]['tel'] = [str(a)]
            except:
                pass
            if '、' in str(df.iloc[row, 1]):
                out['communities'][thisDistrict]['tel'].extend(df.iloc[row, 1].split('、'))
            if '：' in str(df.iloc[row, 1]):
                ct2 += 1
                cn = df.iloc[row, 1][:df.iloc[row, 1].find('：')]
                tel = df.iloc[row, 1][df.iloc[row, 1].find('：') + 1:]
                out['communities'][thisDistrict][cn] = {}
                out['communities'][thisDistrict][cn]['tel'] = tel
                out['communities'][thisDistrict][cn]['loc'] = [-999,-999]

                for d in dL:
                    print(d[0], thisDistrict, cn, d[1])
                    if d[0] in thisDistrict and cn in d[1]:
                        try:
                            del out['communities'][thisDistrict][cn]
                        except:
                            print('wrong---', out['communities'][thisDistrict])
                        out['communities'][thisDistrict][d[1]] = {}
                        out['communities'][thisDistrict][d[1]]['tel'] = tel
                        out['communities'][thisDistrict][d[1]]['loc'] = [d[2], d[3]]
    print('ct222', ct2)
    # with open('out.json','w') as fw:
    #     fw.write(json.dumps(out, separators=(',',':'), ensure_ascii=False))
if __name__ == '__main__':
    main()